package com.bite.system.controller.sysuser;

import com.bite.common.core.constants.HttpConstants;
import com.bite.common.core.controller.BaseController;
import com.bite.common.core.domain.R;
import com.bite.common.core.domain.vo.LoginUserVO;
import com.bite.system.domain.dto.LoginDTO;
import com.bite.system.domain.sysuser.dto.SysUserSaveDTO;
import com.bite.system.domain.sysuser.vo.SysUserVO;
import com.bite.system.service.sysuser.ISysUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/sysUser")
@Tag(name ="管理员接口")
public class SysUserController extends BaseController {
    //请求方法制定url sysuser/login
    @Autowired
    private ISysUserService sysUserService;
    //登录成功/失败 boolean true ,false,int,code 1成功/ 0失败，
    // 并且告诉失败的原因 String(原因) String msg;


    //接口地址, /sysUser/login   安全
    //请求参数POST,请求参数
    //postMapping 要考虑安全性，所以是postMapper，假如是get就会在地址上显示出来
    @PostMapping("/login")        //相应数据，用R进行封装
    @Operation(summary = "管理员登录",description = "根据账号密码进行管理员登录")
    @ApiResponse(responseCode = "1000",description = "操作成功")
    @ApiResponse(responseCode = "2000",description = "服务器繁忙请稍后重试")
    @ApiResponse(responseCode = "3102",description ="用户不存在" )
    @ApiResponse(responseCode = "3103",description ="用户名或者密码错误")
    public R<String> login(@RequestBody LoginDTO loginDTO){
      return sysUserService.login(loginDTO.getUserAccount(),loginDTO.getPassword());
    }
    //管理员的增删改查
    //   接口地址/sysUser/add
    //请求方法
    //新增
    @PostMapping("/add")
    @Operation(summary = "新增管理员",description = "根据提供的信息新增管理员")
    @ApiResponse(responseCode = "1000",description = "操作成功")
    @ApiResponse(responseCode = "2000",description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101",description = "用户已经存在")
    public R<Void> add(@RequestBody SysUserSaveDTO sysUserSaveDTO){
    return  toR(sysUserService.add(sysUserSaveDTO));
    }

    @DeleteMapping("/{userId}")
    @Operation(summary = "删除用户",description = "通过用户ID删除用户")
    @Parameters(value = {
            @Parameter(name="userId",in= ParameterIn.PATH,description = "用户ID")
    })
    @ApiResponse(responseCode = "1000",description = "成功删除用户")
    @ApiResponse(responseCode = "2000",description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101",description = "用户已经存在")
//    PathVariable-上面的Path
    public R<Void> delete(@PathVariable Long userId){
        return  null;
    }

    @Operation(summary = "用户详情",description = "根据查询条件查询用户的详情")
    @GetMapping("/detail")
    @Parameters(value = {
            @Parameter(name="userId",in= ParameterIn.QUERY,description = "用户ID"),
            @Parameter(name = "sex",in=ParameterIn.QUERY,description = "用户性别")
    })
    @ApiResponse(responseCode = "1000",description = "成功获取用户信息")
    @ApiResponse(responseCode = "2000",description = "服务繁忙请稍后重试")
    @ApiResponse(responseCode = "3101",description = "用户已经存在")
    //参数的必须传递和非必须传递  RequestParam用的QUERY
    public R<SysUserVO> delete(@RequestParam(required = true) Long userId, @RequestParam(required = false)String sex){
        return  null;
    }
    //接口实现/system/sysUser/info
    @GetMapping("/info")//相应数据，用R进行封装
    public R<LoginUserVO> login(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        return sysUserService.info(token);
    }
    //接口地址/system/sysUser/logout 退出登录
    @DeleteMapping("logout")
    public R<Void> logout(@RequestHeader(HttpConstants.AUTHENTICATION) String token){
        return toR(sysUserService.logout(token));
    }

}
